#include "stdafx.h"
#include "EsServerNetwork.h"

void EsServerNetwork::onAccept(EssAcceptor* acceptor, EssConnector* connector)
{
	char* ip;
	u_short port;
	if (connector->getPeerName(&ip, &port))
	{
		char log[MAX_PATH] = {0};
		sprintf_s(log, "Accept a new client with IP %s, Port %d.\n", ip, port);
		mLog += log;
	}
}

bool EsServerNetwork::onReceive(EssConnector* connector)
{
	char* ip;
	u_short port;
	if (connector->getPeerName(&ip, &port))
	{
		char log[MAX_PATH] = {0};
		sprintf_s(log, "Recv Msg From Client IP %s, Port %d.\n", ip, port);
		mLog += log;
	}
	char recvBuf[MAX_PATH] = {0};
	sprintf_s(recvBuf, "Recv Msg is %s\n\0", connector->getRecvBuffer().base());
	mLog += recvBuf;
	char retBuf[64] = {0};
	sprintf_s(retBuf, "#NICE TO MEET U TOO CLIENT#\n");
	connector->sendSock(retBuf, strlen(retBuf));
	return true;
}

void EsServerNetwork::onDisconnect(EssConnector* connector)
{
	char* ip;
	u_short port;
	if (connector->getPeerName(&ip, &port))
	{
		char log[MAX_PATH] = {0};
		sprintf_s(log, "A client disconnect with IP %s, Port %d.\n", ip, port);
		mLog += log;
	}
	removeConnector(connector);
}

void EsServerNetwork::broadcast(const char* buf, int len)
{
	for (ConnectorIterator c = mConnectors.begin(); c != mConnectors.end(); ++c)
	{
		c->second->sendSock(buf, len);
	}
}